package com.example.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.SysFinishJobMapper;
import com.example.po.SysFinishJob;
import com.example.po.SysJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysFinishJobServiceImpl extends ServiceImpl<SysFinishJobMapper, SysFinishJob> implements SysFinishJobService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${sys.transfer.job.size}")
    private int pollSize;

    @Resource
    private SysJobService sysJobService;

    @Override
    public void updateAndTransfer() {
        List<SysJob> sysJobs = sysJobService.getFinishJobs(pollSize);
        for (SysJob sysJob : sysJobs) {
            SysFinishJob sysFinishJob = new SysFinishJob();
            BeanUtils.copyProperties(sysJob, sysFinishJob);
            sysFinishJob.recalculateFields();
            this.save(sysFinishJob);
            sysJobService.removeById(sysJob.getId());
        }
    }
}
